//
//  PfV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/21.
//

import Foundation

class PfV: UIView, UITextViewDelegate {
    
    var bgView: UIView!
    
    var pf: PfLine!
    
    var bz: UITextView!
    var bz_p: UILabel!
    
    var canEdit: Bool = true
    var rating: Int = 1
    var remark: String = ""
    
    var callBack: ((PfV?, String, [String: Any]) -> Void)!
    
    func showIn(_ SV: UIView, _ rating: Int, _ remark: String, _ canEdit: Bool, _ callBack: @escaping (PfV?, String, [String: Any]) -> Void) -> Void {
        self.clipsToBounds = true
        self.callBack = callBack
        self.canEdit = canEdit
        self.rating = rating
        self.remark = remark
        SV.addSubview(self)
        lgf_FillSuperview()
        
        config()
        
        bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.3) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() {
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.clipsToBounds = true
        bgView.layer.cornerRadius = 5
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalTo(230)
        }
        
        let label1 = UILabel.init()
        label1.text = "请评分"
        label1.font = UIFont.boldSystemFont(ofSize: 16)
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        label1.textAlignment = .center
        bgView.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(45)
        }
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.init(lgf_HexString: "f0f0f0")
        bgView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-45)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        let line2 = UIView.init()
        line2.backgroundColor = UIColor.init(lgf_HexString: "f0f0f0")
        bgView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview().offset(self.canEdit ? 0 : 150)
            make.width.equalTo(0.5)
        }
        
        let qx = UILabel.init()
        qx.lgf_AddTap(target: self, action: #selector(PfV.s_qx))
        qx.text = "取消"
        qx.textAlignment = .center
        qx.textColor = UIColor.init(lgf_HexString: "8D969D")
        bgView.addSubview(qx)
        qx.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.left.bottom.equalToSuperview()
            make.right.equalTo(line2.snp.left)
        }
        
        let qd = UILabel.init()
        qd.lgf_AddTap(target: self, action: #selector(PfV.s_qd))
        qd.text = "确定"
        qd.textAlignment = .center
        qd.textColor = UIColor.init(lgf_HexString: "0F88F2")
        bgView.addSubview(qd)
        qd.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.right.bottom.equalToSuperview()
            make.left.equalTo(line2.snp.right)
        }
        
        pf = PfLine.init()
        pf.isUserInteractionEnabled = self.canEdit
        bgView.addSubview(pf)
        pf.snp.makeConstraints { (make) in
            make.top.equalTo(label1.snp.bottom)
            make.width.equalToSuperview().offset(-40)
            make.centerX.equalToSuperview()
            make.height.equalTo(40)
        }
        pf.select(min(max(rating, 1), 10) - 1)
        
        bz = UITextView.init()
        bz.delegate = self
        bz.text = self.remark
        bz.isUserInteractionEnabled = self.canEdit
        bz.font = UIFont.systemFont(ofSize: 15)
        bz.backgroundColor = UIColor.init(lgf_HexString: "F6F9FB")
        bgView.addSubview(bz)
        bz.snp.makeConstraints { (make) in
            make.top.equalTo(pf.snp.bottom).offset(15)
            make.width.equalToSuperview().offset(-40)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(line1.snp.top).offset(-15)
        }
        bz_p = UILabel.init()
        bz_p.text = "请填写备注"
        bz_p.isHidden = !self.canEdit || self.remark.count > 0
        bz_p.font = UIFont.systemFont(ofSize: 15)
        bz_p.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        bgView.addSubview(bz_p)
        bz_p.snp.makeConstraints { (make) in
            make.top.equalTo(pf.snp.bottom).offset(23)
            make.left.equalToSuperview().offset(26)
        }
        
        
    }
    
    @objc func s_qx() {
        remove()
    }
    
    @objc func s_qd() {
        remove()
        callBack?(self, "", ["pf": pf.pf_tag, "bz": bz.text ?? ""])
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.3, animations: {
            self.bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.bgView.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
    func textViewDidChange(_ textView: UITextView) {
        bz_p.isHidden = textView.text.count > 0
    }
    
}

class PfLine: UIView {
    
    var play_line: UISlider!
    var view1: UIView!
    var view2: UIView!
    var view3: UIView!
    var view4: UIView!
    var view5: UIView!
    var view6: UIView!
    var view7: UIView!
    var view8: UIView!
    var view9: UIView!
    var view10: UIView!
    
    var views: [UIView]!
    
    var pfsm: UILabel!
    
    var pf_tag: Int = 1
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        views = []
        for i in 0..<10 {
            let view = UIView.init()
            view.layer.cornerRadius = 4
            if i == 0 {
                view.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            } else {
                view.backgroundColor = UIColor.init(lgf_HexString: "EAEEF2")
            }
            addSubview(view)
            view.snp.makeConstraints { (make) in
                make.left.equalTo(CGFloat(i) * (CGFloat(252.0 / 9.0)))
                make.centerY.equalToSuperview().offset(1)
                make.width.height.equalTo(8)
            }
            views.append(view)
        }
        
        play_line = UISlider.init()
        play_line.minimumValue = 0//最小值
        play_line.maximumValue = 1//最大值
        play_line.value = 0 //当前值
        play_line.minimumTrackTintColor = UIColor.init(lgf_HexString: "0F88F2")
        play_line.maximumTrackTintColor = UIColor.init(lgf_HexString: "EAEEF2")
        play_line.thumbTintColor = UIColor.init(lgf_HexString: "0F88F2")
        let thumbImage = #imageLiteral(resourceName: "play_i")
        UIGraphicsBeginImageContextWithOptions(CGSize.init(width: 15, height: 15), false, 0.0)
        thumbImage.draw(in: CGRect.init(x: 0, y: 0, w: 15, h: 15))
        let newThumbImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        play_line.setThumbImage(newThumbImage, for: .normal)
        play_line.setThumbImage(newThumbImage, for: .selected)
        play_line.setThumbImage(newThumbImage, for: .highlighted)
        play_line.setThumbImage(newThumbImage, for: .focused)
        play_line.isUserInteractionEnabled = false
        addSubview(play_line)
        play_line.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(30)
        }
        
        for (i, v) in views.enumerated() {
            let view_b = UIView.init()
            view_b.lgf_ViewName = i.description
            view_b.lgf_AddTap(target: self, action: #selector(s_tap(_:)))
            addSubview(view_b)
            view_b.snp.makeConstraints { (make) in
                make.center.equalTo(v)
                make.width.height.equalTo(40)
            }
        }
        
        let left = UILabel.init()
        left.text = "1"
        left.textColor = UIColor.init(lgf_HexString: "8D969D")
        left.font = UIFont.systemFont(ofSize: 12)
        addSubview(left)
        left.snp.makeConstraints { (make) in
            make.centerX.equalTo(views.first!)
            make.top.equalTo(views.first!.snp.bottom).offset(5)
        }
        
        let right = UILabel.init()
        right.text = "10"
        right.textColor = UIColor.init(lgf_HexString: "8D969D")
        right.font = UIFont.systemFont(ofSize: 12)
        addSubview(right)
        right.snp.makeConstraints { (make) in
            make.centerX.equalTo(views.last!)
            make.top.equalTo(views.first!.snp.bottom).offset(5)
        }
        
        pfsm = UILabel.init()
        pfsm.text = ""
        pfsm.textColor = UIColor.init(lgf_HexString: "0F88F2")
        pfsm.font = UIFont.boldSystemFont(ofSize: 12)
        addSubview(pfsm)
        pfsm.snp.makeConstraints { (make) in
            make.centerX.equalTo(views[views.count - 3])
            make.bottom.equalTo(views[views.count - 3].snp.top).offset(-6)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func s_tap(_ sender: UITapGestureRecognizer) {
        let view = sender.view
        let tag = (Int((view?.lgf_ViewName!)!) ?? 0)
        select(tag)
    }
    
    func select(_ tag: Int) {
        if tag < 0 {
            return
        }
        pf_tag = tag + 1
        play_line.setValue(Float(tag) / 9.0, animated: false)
        for (i, v) in views.enumerated() {
            if i < tag {
                v.backgroundColor = UIColor.init(lgf_HexString: "0F88F2")
            } else {
                v.backgroundColor = UIColor.init(lgf_HexString: "EAEEF2")
            }
        }
        pfsm.snp.removeConstraints()
        pfsm.snp.updateConstraints { (make) in
            if tag == 9 {
                make.centerX.equalTo(views[tag])
            } else {
                make.centerX.equalTo(views[tag])
            }
            make.bottom.equalTo(views[tag].snp.top).offset(-6)
        }
        var type = "优秀"
        if pf_tag >= 1 && pf_tag <= 3 {
            type = "差"
        } else if pf_tag >= 4 && pf_tag <= 6 {
            type = "良好"
        } else if pf_tag >= 7 && pf_tag <= 10 {
            type = "优秀"
        }
        pfsm.text = (tag + 1).description + type
    }
    
}
